<template>
  <div class="title">
<!--    <h3>添加员工</h3>-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin/employee' }">员工管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加员工</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div class="add_employee">
    <div class="container">
      <div class="fill_info">
        <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            :rules="rules"
            label-width="auto"
            class="demo-ruleForm"
            :size="formSize"
            status-icon
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username" placeholder="请输入账号"/>
          </el-form-item>
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入员工姓名"/>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="ruleForm.phone" placeholder="手机号"/>
          </el-form-item>

          <el-form-item label="性别：" prop="phone">
          <el-radio-group v-model="ruleForm.sex">
            <el-radio :value="1" size="large">男</el-radio>
            <el-radio :value="0" size="large">女</el-radio>
          </el-radio-group>
          </el-form-item>
          <el-form-item label="身份证" prop="idNumber">
            <el-input v-model="ruleForm.idNumber" placeholder="身份证"/>
          </el-form-item>
        </el-form>
      </div>

      <div class="handleOperation">
        <el-button type="primary"  @click="submitForm(ruleFormRef)">保存</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef,false)">保存并继续添加员工</el-button>
        <el-button type="primary">返回</el-button>

      </div>

    </div>
  </div>


</template>

<script setup>
import { reactive,ref } from 'vue';
import {addEmployee} from "@/api/admin-api.js";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
const ruleForm = reactive({
  username:"测试账号0",
  name:"测试员工0",
  phone:'12345679812',
  sex:1,
  idNumber:"123456789123456789",
})

const ruleFormRef = ref()
const rules = reactive({
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 3, max: 10, message: 'Length should be 3 to 10', trigger: 'blur' },
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' }
  ],
  idNumber: [
    { required: true, message: '请输入身份证号  ', trigger: 'blur' }
  ],

})

const router = useRouter()

const submitForm = async (formEl,locate = true) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      console.log(ruleForm);
      try {
        const res = await addEmployee(ruleForm);
        console.log(res.data);
        console.log('submit!')
        ElMessage.success("添加成功")
        if(locate){
          await router.push('/admin/employee')
        }else {
          ruleForm.username = ''
          ruleForm.name = ""
          ruleForm.phone = ''
          ruleForm.sex = 1
          ruleForm.idNumber = ''
        }
      }catch (e){
        ElMessage.error("添加失败："+e.message)
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

</script>
<style lang="scss" scoped>
.title{
  height: 50px;
  width: 100%;
  background-color: #fff;
}
.add_employee{
  margin-top: 10px;
  width:100%;
  height:400px;
  background-color: #f3f3f3;
  display:flex;
  justify-content: center;
  align-items: center;

  .container{
    width:97%;
    height:95%;
    background-color:white;
    padding: 20px;
    box-sizing: border-box;
    .handleOperation{
      width: 100%;
      height: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>